#include "../include/import.h"
#include "../include/PDTextExtractor.h"
#include "../include/FXAPIModule.h"
#include <stdio.h>
#include <iostream>
using namespace std;

PDF2PDFMoudleMgr FP2P_CreateModuleMgr()
{
	CFXAPIModule* p = new CFXAPIModule;
	p->Create();
	return (PDF2PDFMoudleMgr)p;
}
void FP2P_DestroyModuleMgr(PDF2PDFMoudleMgr mgr)
{
	CFXAPIModule* p = (CFXAPIModule*)mgr;
	p->Destroy();
	delete p;
}

PDF2PDFProcessor FP2P_NewProcessor()
{
try{
	CPDProccessor* pProcessor = new CPDProccessor();
	pProcessor->m_Builder.SetTextExtractor(&pProcessor->m_Extractor);
	return pProcessor;
}catch(...)
{
	return NULL;
}
	
}
void FP2P_CloseProcessor(PDF2PDFProcessor Proccessor)
{
	CPDProccessor* pProcessor = (CPDProccessor*)Proccessor;
	pProcessor->m_Extractor.Close();
	delete pProcessor;
}
int FP2P_StartExtract(PDF2PDFProcessor Proccessor, const wchar_t* file, const char* pwd)
{
try{
	CPDProccessor* pProcessor = (CPDProccessor*)Proccessor;
	return pProcessor->m_Extractor.StartExtract((FX_LPCWSTR)file, pwd);
}catch(...)
{
	return -1;
}
	
}


int FP2P_CountPage(PDF2PDFProcessor Proccessor)
{
try{
	CPDProccessor* pProcessor = (CPDProccessor*)Proccessor;
	return pProcessor->m_Extractor.CountPage();
}catch(...){
	return 0;
}
}
int FP2P_CountParagraph(PDF2PDFProcessor Proccessor, int iPage)
{
try{
	CPDProccessor* pProcessor = (CPDProccessor*)Proccessor;
	return pProcessor->m_Extractor.CountParagraphRects(iPage);
}catch(...){return 0;}
}

int FP2P_IsAllLineIsIndepend(PDF2PDFProcessor Proccessor, int iPage, int iParagraph)
{
CPDProccessor* pProcessor = (CPDProccessor*)Proccessor;
if(pProcessor->m_Extractor.IsAllLineIndepend(iPage, iParagraph)) return 1;
return 0;
}
int FP2P_CountIndependLineInParagraph(PDF2PDFProcessor Proccessor, int iPage, int iParagraph)
{
CPDProccessor* pProcessor = (CPDProccessor*)Proccessor;
return pProcessor->m_Extractor.GetIndependLineCount(iPage, iParagraph);
}
PDFWStringContent FP2P_GetIndependLineContent(PDF2PDFProcessor Proccessor, int iPage, int iParagraph, int iLine)
{
CPDProccessor* pProcessor = (CPDProccessor*)Proccessor;
CFX_WideString* pW = FX_NEW CFX_WideString;
	*pW = pProcessor->m_Extractor.GetIndependLineContent(iPage, iParagraph, iLine);
return pW;
}

PDFWStringContent FP2P_GetParagraphContent(PDF2PDFProcessor Proccessor, int iPage, int iParagraph)
{
	CPDProccessor* pProcessor = (CPDProccessor*)Proccessor;
	CFX_WideString* pW = FX_NEW CFX_WideString;
	*pW = pProcessor->m_Extractor.GetParagraphContent(iPage, iParagraph);
	return pW;
}
const wchar_t* FP2P_GetContent(PDFWStringContent content, int& nLen)
{
	CFX_WideString* pW = (CFX_WideString*)content;
	nLen = pW->GetLength();
	return (FX_LPCWSTR)(*pW); 
}
void FP2P_FreeContent(PDFWStringContent content)
{

	CFX_WideString* pW = (CFX_WideString*)content;
	delete pW;

}
void FP2P_InitDestInfo(PDF2PDFProcessor Proccessor, int charset)
{
	CPDProccessor* pProcessor = (CPDProccessor*)Proccessor;
	pProcessor->m_Builder.InitDestInfo(charset);
}

void FP2P_AddParagraph(PDF2PDFProcessor Proccessor, int iPage, int iParagraph, const wchar_t* content)
{

	CPDProccessor* pProcessor = (CPDProccessor*)Proccessor;
	CFX_WideString ws((FX_LPCWSTR)content);
try{
	pProcessor->m_Builder.AddParagraph(iPage, iParagraph, ws);
}catch(...){}
}

void FP2P_AddIndependLine(PDF2PDFProcessor Proccessor, int iPage, int iParagraph, int iLine, const wchar_t* content)
{
	CPDProccessor* pProcessor = (CPDProccessor*)Proccessor;
	CFX_WideString ws((FX_LPCWSTR)content);
try{
	pProcessor->m_Builder.AddIndependLine(iPage, iParagraph, iLine, ws);
}catch(...){}
}
int FP2P_SaveFile(PDF2PDFProcessor Proccessor, const wchar_t* file)
{
try{
	CPDProccessor* pProcessor = (CPDProccessor*)Proccessor;
	CFX_WideString ws((FX_LPCWSTR)file, -1);
	return pProcessor->m_Builder.Save(ws);
}catch(...){return -1;}
}

	
